package com.woniuxy.sale.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.sale.PackingList;
import com.woniuxy.commons.entity.sale.PackingMsg;
import com.woniuxy.commons.entity.sale.dto.ChooseProduceMsgDto;
import com.woniuxy.commons.rabbit.GoldService;
import com.woniuxy.commons.rabbit.RabbitMqConst;
import com.woniuxy.commons.result.Result;
import com.woniuxy.commons.utils.DictUtils;
import com.woniuxy.sale.mapper.AmountDao;
import com.woniuxy.sale.service.AmountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AmountServiceImpl implements AmountService {

    @Resource
    private AmountDao amountDao;

    @Resource
    private GoldService goldService;

    /**
     * 查找打包状态下未打包状态的所有列表
     * */
    @Override
    public Result findStatusList(Integer currentPage, Integer limit) {
        PageHelper.startPage(currentPage,limit);
        //打包状态为未审核以及进行中的状态时要进入结算页面
            List<PackingList> list = amountDao.findStatusList();
            PageInfo<?> of= PageInfo.of(list);
            return Result.ok(of);
    }


    @Override
    public Map<String, Object> findPackingMsgByPackngId(String packngId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<PackingMsg> list = amountDao.findPackingMsgByPackngId(packngId);
        map.put("PackingMsg",list);
        return map;
    }

    @Override
    public Map<String, Object> findPackingMsgByPsId(Integer psId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<ChooseProduceMsgDto> list = amountDao.findChooseProducesByPsId(psId);
        List<?> objects = DictUtils.setListDict(list);
        map.put("ChooseProduceMsgDto",objects);
        return map;
    }

    @Override
    public boolean updateChooseProduceStatusByPsIdAndId(Map<String,Object>  map) {

        Integer psId = JSON.parseObject((String) map.get("psId"), Integer.class);
        List ids = (List) (map.get("ids"));
        int i =  amountDao.updateChooseProduceStatusByPsIdAndId(psId,ids);
        if(i > 0){
            goldService.send(psId,
                    RabbitMqConst.EXCHANGE_DIRECT_PRODUCT_UPDATE,
                    RabbitMqConst.ROUTING_CHOOSE_PRODUCE_MSG);
        }
        return  i > 0;
    }


}
